﻿Imports System
Imports System.Collections.Generic
Imports System.ComponentModel
Imports System.Text
Imports System.Web
Imports System.Web.UI
Imports System.Web.UI.WebControls

Namespace DotNetNuke.Modules.ActiveForums.Controls
    <DefaultProperty("Text"), ToolboxData("<{0}:Modal runat=server></{0}:Modal>")> _
    Public Class Modal
        Inherits CompositeControl
        Protected WithEvents cb As New Controls.Callback
        Protected WithEvents plh As New PlaceHolder
        Private _CallBackOnComplete As String = String.Empty

        <Bindable(True), Category("Appearance"), DefaultValue(""), Localizable(True)> Property Text() As String
            Get
                Dim s As String = CStr(ViewState("Text"))
                If s Is Nothing Then
                    Return String.Empty
                Else
                    Return s
                End If
            End Get

            Set(ByVal Value As String)
                ViewState("Text") = Value
            End Set
        End Property
        Property CallBackOnComplete() As String
            Get
                Return _CallBackOnComplete
            End Get
            Set(ByVal value As String)
                _CallBackOnComplete = value
            End Set
        End Property
        Property ModalContent() As PlaceHolder
            Get
                EnsureChildControls()
                If plh Is Nothing Then
                    plh = New PlaceHolder
                End If
                Return plh
            End Get
            Set(ByVal value As PlaceHolder)
                plh = value
            End Set
        End Property
        Protected Overrides Sub CreateChildControls()
            cb = New Callback
            cb.ID = "CB_" & Me.ClientID
            If HttpContext.Current.Request.Params("amtsdebug") = "true" Or HttpContext.Current.Request.Params("amdebug") = "true" Then
                cb.Debug = True
            Else
                cb.Debug = False
            End If
            If Not CallBackOnComplete = String.Empty Then
                cb.OnCallbackComplete = CallBackOnComplete
            End If
            Dim cnt As New Controls.CallBackContent
            cnt.Controls.Add(ModalContent)
            cb.Content = cnt
            Me.Controls.Add(cb)
            ChildControlsCreated = True
        End Sub
        Protected Overrides Sub RenderChildren(ByVal writer As System.Web.UI.HtmlTextWriter)
            Dim childControl As Control
            For Each childControl In Controls
                childControl.RenderControl(writer)
            Next
        End Sub
        Protected Overrides Sub Render(ByVal writer As HtmlTextWriter)
            With writer
                'BEGIN MASK
                .AddAttribute("id", "amModalMask")
                .AddAttribute("style", "display:none;width:100%;background-color:Gray;top:0;left:0;position:absolute;opacity: 0.6;-moz-opacity: 0.6;filter: alpha(opacity=60);")
                .RenderBeginTag(HtmlTextWriterTag.Div)
                .RenderEndTag()
                'END MASK
                'BEGIN MAIN DIV
                '<div id="amModal" class="amModal" style="display:none;position:absolute;">
                .AddAttribute("id", "amModal")
                .AddAttribute("class", "amModal")
                .AddAttribute("style", "display:none;position:absolute;")
                .RenderBeginTag(HtmlTextWriterTag.Div)
                '   BEGIN HEADER
                '<div class="amModalHeader">
                .AddAttribute("class", "amModalHeader")
                .RenderBeginTag(HtmlTextWriterTag.Div)
                '       BEGIN HEADER TEXT
                '    <div class="amModalHeaderText" id="amModalHeaderText"></div>
                .AddAttribute("class", "amModalHeaderText")
                .AddAttribute("id", "amModalHeaderText")
                .RenderBeginTag(HtmlTextWriterTag.Div)
                .RenderEndTag()
                '       END HEADER TEXT
                '       BEGIN CLOSE BUTTON
                '    <div class="amModalCloseImg" onclick="amaf_closeDialog();"><img src="<%=Page.ResolveUrl("~/DesktopModules/activeforums/images/close.gif") %>" alt="[RESX:Close]" /></div>
                .AddAttribute("class", "amModalCloseImg")
                .AddAttribute("onclick", "amaf_closeDialog();")
                .RenderBeginTag(HtmlTextWriterTag.Div)
                .AddAttribute("src", Page.ResolveUrl("~/DesktopModules/activeforums/images/close.gif"))
                .AddAttribute("alt", "[RESX:Close]")
                .RenderBeginTag(HtmlTextWriterTag.Img)
                .RenderEndTag() 'Close image
                .RenderEndTag() 'Close div
                '   END Close Button
                .RenderEndTag() 'Close Header
                'END HEADER
                'BEGIN Content AREA
                '<div class="amModalFrame" id="amModalFrameDiv">
                .AddAttribute("class", "amModalFrame")
                .AddAttribute("id", "amModalFrameDiv")
                .RenderBeginTag(HtmlTextWriterTag.Div)
                cb.RenderControl(writer)
                .RenderEndTag() 'Close Content Area
                'END Content AREA
                'END Main Div
                .RenderEndTag() ' Close Main Div



            End With
            Dim sb As New Text.StringBuilder
            sb.Append("<script type=""text/javascript"">function afam_showDialog(title,key,height,width,optional){")
            sb.Append("function getTop(obj,height){")
            sb.Append("var dlg=document.getElementById(obj);")
            sb.Append("var winH=document.body.clientHeight;")
            sb.Append("var top=((winH/2)-(height/2));")
            sb.Append("dlg.style.top=top+'px';")
            sb.Append("};")
            sb.Append("function getLeft(obj,width){")
            sb.Append("var dlg=document.getElementById(obj);")
            sb.Append("var winW=document.body.offsetWidth;")
            sb.Append("dlg.style.left=((winW/2)-(width/2))+'px';")
            sb.Append("};")
            sb.Append("dlgHeight=height;")
            sb.Append("dlgWidth=width;")
            sb.Append("        dlgTitle=title;")
            sb.Append("        getTop('amModal',height);")
            sb.Append("        getLeft('amModal',width);")
            sb.Append(" var mask = document.getElementById('amModalMask');")
            sb.Append("        mask.style.zIndex=200000;")
            sb.Append("        mask.style.height=document.body.offsetHeight;")
            sb.Append("        mask.style.width=document.body.offsetWidth-22;")
            sb.Append("        mask.style.display='';")
            sb.Append("    var modal = document.getElementById('amModal');")
            sb.Append("     var modalFrameDiv = document.getElementById('amModalFrameDiv');")
            sb.Append("        modal.style.zIndex=200001;")
            sb.Append("        modal.style.height=dlgHeight+'px';")
            sb.Append("        modal.style.width=dlgWidth+'px';  ")
            sb.Append("        modal.style.display='';")
            sb.Append("        modalFrameDiv.style.height=height-22+'px';")
            sb.Append("        var amModalHeader=document.getElementById('amModalHeaderText');")
            sb.Append("        amModalHeader.innerHTML=dlgTitle;")
            sb.Append("        if(optional=='undefined'){optional=''};")
            sb.Append("         var modFrame = document.getElementById('amModalFrame');")
            sb.Append("            if (modFrame != undefined){")
            sb.Append("                modFrame.height = '0';")
            sb.Append("            };")
            sb.Append("        " & cb.ClientID & ".Callback('load',key,optional);")
            sb.Append("};")
            sb.Append("function amaf_closeDialog(){")
            sb.Append("        var modFrame = document.getElementById('amModalFrame');")
            sb.Append("        if (modFrame != undefined){")
            sb.Append("            modFrame.height = '0';")
            sb.Append("           modFrame.parentNode.removeChild(modFrame);")
            sb.Append("        };")
            sb.Append("        var dlg=document.getElementById('amModal');")
            sb.Append("        dlg.style.display='none';")
            sb.Append(" var mask = document.getElementById('amModalMask');")
            sb.Append("        var cModal = document.getElementById('" & cb.ClientID & "');")
            sb.Append("        cModal.removeChild(cModal.firstChild);")
            sb.Append("        mask.style.display='none';")
            sb.Append("};</script>")
            writer.Write(sb.ToString)


        End Sub
        Public Event Callback(ByVal sender As Object, ByVal e As CallBackEventArgs)
        Public Sub RaiseCallback(ByVal sender As Object, ByVal e As CallBackEventArgs) Handles cb.Callback ' Implements ICallbackEventHandler.RaiseCallback
            OnCallback(e)
        End Sub
        Protected Overridable Sub OnCallback(ByVal e As CallBackEventArgs)
            RaiseEvent Callback(cb, e)
        End Sub


        Private Sub Modal_Init(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Init

        End Sub
    End Class
End Namespace